package 链表;

public class Demo08 {
    /*
    * 评价: 太不优雅了
    * */
    static class Solution {
        public ListNode rotateRight(ListNode head, int k) {
            if (head == null || k == 0 || head.next == null) {
                return head;
            }

            int count = 0;
            ListNode cur = head;
            ListNode tail = null;
            while (cur != null) {
                tail = cur;
                cur = cur.next;
                count++;
            }
            k = k % count;
            if (k == 0) {
                return head;
            }

            ListNode pre = null;
            cur = head;
            while (count - k > 0) {
                pre = cur;
                cur = cur.next;
                count--;
            }
            pre.next = null;
            tail.next = head;
            return cur;
        }
    }
}
